{- This file is part of funbot.
 -
 - Written in 2016, 2017 by fr33domlover <fr33domlover@riseup.net>.
 -
 - ♡ Copying is an act of love. Please copy, reuse and share.
 -
 - The author(s) have dedicated all copyright and related and neighboring
 - rights to this software to the public domain worldwide. This software is
 - distributed without any warranty.
 -
 - You should have received a copy of the CC0 Public Domain Dedication along
 - with this software. If not, see
 - <http://creativecommons.org/publicdomain/zero/1.0/>.
 -}

{-# LANGUAGE OverloadedStrings #-}

module FunBot.Migration
    ( migrateDB
    )
where

import Control.Monad (unless)
import Control.Monad.IO.Class (MonadIO)
import Control.Monad.Trans.Reader (ReaderT, runReaderT)
import Data.Maybe (fromMaybe)
import Data.Proxy
import Data.Text (Text)
import Data.Time.Clock (UTCTime)
import Database.Persist.Class
import Database.Persist.Sql (SqlBackend, toSqlKey, sqlType)
import Network.IRC.Fun.Types.Base (Nickname, Channel, MsgContent)

import Database.Persist.Schema
import Database.Persist.Schema.PostgreSQL (schemaBackend)
import Database.Persist.Migration

import FunBot.Model
import FunBot.Types

changes :: MonadIO m => [SchemaT SqlBackend m ()]
changes =
    [ addEntity Entity
        { entityName    = "Memo"
        , entityFields  =
            [ Field
                { fieldName = "time"
                , fieldType = FTPrim $ sqlType (Proxy :: Proxy UTCTime)
                , fieldNull = NotNull
                }
            , Field
                { fieldName = "sender"
                , fieldType = FTPrim $ sqlType (Proxy :: Proxy Nickname)
                , fieldNull = NotNull
                }
            , Field
                { fieldName = "recip"
                , fieldType = FTPrim $ sqlType (Proxy :: Proxy Nickname)
                , fieldNull = NotNull
                }
            , Field
                { fieldName = "recvIn"
                , fieldType = FTPrim $ sqlType (Proxy :: Proxy Channel)
                , fieldNull = MaybeNull
                }
            , Field
                { fieldName = "sendIn"
                , fieldType = FTPrim $ sqlType (Proxy :: Proxy Channel)
                , fieldNull = MaybeNull
                }
            , Field
                { fieldName = "content"
                , fieldType = FTPrim $ sqlType (Proxy :: Proxy MsgContent)
                , fieldNull = NotNull
                }
            , Field
                { fieldName = "ask"
                , fieldType = FTPrim $ sqlType (Proxy :: Proxy Bool)
                , fieldNull = NotNull
                }
            ]
        , entityUniques = []
        }
    , addEntity Entity
        { entityName    = "Kith"
        , entityFields  =
            [ Field
                { fieldName = "channel"
                , fieldType = FTPrim $ sqlType (Proxy :: Proxy Channel)
                , fieldNull = NotNull
                }
            , Field
                { fieldName = "nickname"
                , fieldType = FTPrim $ sqlType (Proxy :: Proxy Nickname)
                , fieldNull = NotNull
                }
            ]
        , entityUniques =
            [ Unique
                { uniqueName   = "UniqueKith"
                , uniqueFields = ["channel", "nickname"]
                }
            ]
        }
    , addEntity Entity
        { entityName    = "GlobalLocation"
        , entityFields  =
            [ Field
                { fieldName = "label"
                , fieldType = FTPrim $ sqlType (Proxy :: Proxy LocationLabel)
                , fieldNull = NotNull
                }
            , Field
                { fieldName = "content"
                , fieldType = FTPrim $ sqlType (Proxy :: Proxy LocationText)
                , fieldNull = NotNull
                }
            ]
        , entityUniques =
            [ Unique
                { uniqueName   = "UniqueGlobalLocation"
                , uniqueFields = ["label"]
                }
            ]
        }
    , addEntity Entity
        { entityName    = "LocalLocation"
        , entityFields  =
            [ Field
                { fieldName = "channel"
                , fieldType = FTPrim $ sqlType (Proxy :: Proxy Channel)
                , fieldNull = NotNull
                }
            , Field
                { fieldName = "label"
                , fieldType = FTPrim $ sqlType (Proxy :: Proxy LocationLabel)
                , fieldNull = NotNull
                }
            , Field
                { fieldName = "content"
                , fieldType = FTPrim $ sqlType (Proxy :: Proxy LocationText)
                , fieldNull = NotNull
                }
            ]
        , entityUniques =
            [ Unique
                { uniqueName   = "UniqueLocalLocation"
                , uniqueFields = ["channel", "label"]
                }
            ]
        }
    , addEntity Entity
        { entityName    = "Folk"
        , entityFields  =
            [ Field
                { fieldName = "channel"
                , fieldType = FTPrim $ sqlType (Proxy :: Proxy Channel)
                , fieldNull = NotNull
                }
            , Field
                { fieldName = "nickname"
                , fieldType = FTPrim $ sqlType (Proxy :: Proxy Nickname)
                , fieldNull = NotNull
                }
            ]
        , entityUniques =
            [ Unique
                { uniqueName   = "UniqueFolk"
                , uniqueFields = ["channel", "nickname"]
                }
            ]
        }
    , addField "Kith" (Just "TRUE") Field
        { fieldName = "spoke"
        , fieldType = FTPrim $ sqlType (Proxy :: Proxy Bool)
        , fieldNull = NotNull
        }
    , addEntity Entity
        { entityName    = "Shortcut"
        , entityFields  =
            [ Field
                { fieldName = "label"
                , fieldType = FTPrim $ sqlType (Proxy :: Proxy ShortcutLabel)
                , fieldNull = NotNull
                }
            , Field
                { fieldName = "prefix"
                , fieldType = FTPrim $ sqlType (Proxy :: Proxy Text)
                , fieldNull = NotNull
                }
            , Field
                { fieldName = "before"
                , fieldType = FTPrim $ sqlType (Proxy :: Proxy Text)
                , fieldNull = NotNull
                }
            , Field
                { fieldName = "after"
                , fieldType = FTPrim $ sqlType (Proxy :: Proxy Text)
                , fieldNull = NotNull
                }
            ]
        , entityUniques =
            [ Unique
                { uniqueName   = "UniqueShortcut"
                , uniqueFields = ["label"]
                }
            ]
        }
    , addEntity Entity
        { entityName    = "ShortcutChannel"
        , entityFields  =
            [ Field
                { fieldName = "shortcut"
                , fieldType = FTRef
                , fieldNull = NotNull
                }
            , Field
                { fieldName = "channel"
                , fieldType = FTPrim $ sqlType (Proxy :: Proxy Channel)
                , fieldNull = NotNull
                }
            ]
        , entityUniques =
            [ Unique
                { uniqueName   = "UniqueShortcutChannel"
                , uniqueFields = ["shortcut", "channel"]
                }
            ]
        }
    ]

migrateDB :: MonadIO m => ReaderT SqlBackend m ()
migrateDB = runMigrations schemaBackend changes
